home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_08_03 / 8n03051a < prev    next >
Text File  |  1990-03-18  |  1KB  |  70 lines

  1. *****Listing 3*****
  2.  
  3.  
  4. // remember:  typedef char T ...
  5. class Yacht : public Sloop {
  6. public:
  7.     // return current offset of point
  8.     int where()
  9.     {
  10.         int r = lengthb();
  11.         go( r);
  12.         return r;
  13.     }
  14.  
  15.     // go moves point to desired offset.  If go fails, it leaves
  16.     // point at end of yacht
  17.     Truth go( int n)
  18.     {
  19.         begin();
  20.         for( ; !isend() && (n >= 0); next() )
  21.             n--;
  22.         return !n;
  23.     }
  24.  
  25.     // get() returns value after point and advances the point
  26.     // pre-condition:  !isend()
  27.     T get()
  28.     {
  29.         T r;
  30.         r = geta();
  31.         next();
  32.         return r;
  33.     }
  34.  
  35.     // position point at beginning
  36.     void begin()  { while( !isbegin() ) prev(); }
  37.  
  38.     // position point at end
  39.     void end()    { while( !isend() ) next(); }
  40.  
  41.     // return number of elements in Yacht
  42.     int length()
  43.     {
  44.         int r = lengthb();
  45.         for( int n = 0; !isend(); next() )
  46.             n++;
  47.         go( r);
  48.         return n;
  49.     }
  50.  
  51.     // print Yacht, from point to end, to standard I/O FILE
  52.     void print( FILE *fp = stdout)
  53.     {
  54.         while( !isend() ) {
  55.             fprintf( fp, "%d\n", geta() );
  56.             next();
  57.         }
  58.     }
  59. private:
  60.     // lengthb - moves  point to beginning and returns
  61.     // previous offset of point
  62.     int lengthb()
  63.     {
  64.         for( int n = 0; !isbegin(); prev() )
  65.             n++;
  66.         return n;
  67.     }
  68. };
  69.  
  70.